﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Guest
{
    public partial class iShopping : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Timeout = 100;

            

            if (IsPostBack == false)
            {
                List<LoaiSanPhamDTO> loaiSanPhamDtos = LoaiSanPhamBUS.LayDSLoaiSanPham();

                dllist.Items.Add(new ListItem("Tất Cả Danh Mục", "LSP000"));
                foreach (LoaiSanPhamDTO loaiSanPhamDto in loaiSanPhamDtos)
                {
                    dllist.Items.Add(new ListItem(loaiSanPhamDto.TenLoaiSanPham, loaiSanPhamDto.MaLoaiSanPham));
                }
                dllist.SelectedIndex = 0;
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string sanpham = TextBox1.Text;
            Response.Redirect("~/Guest/KetQuaTimKiem.aspx?name=" + sanpham + "&id=" + dllist.SelectedItem.Value + "&pgn=1");
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            TaiKhoanDTO tk = new TaiKhoanDTO();
            tk.TenTaiKhoan = tb_username.Value;
            tk.MatKhau = tb_password.Value;
            bool result = TaiKhoanBUS.checkTaiKhoan(tk);
            string maTaiKhoan = TaiKhoanBUS.GetTaiKhoanOfTens(tk.TenTaiKhoan);
            string maLoaiTK = TaiKhoanBUS.LayMaLoaiTK(tk.TenTaiKhoan);
            if (result == true && maLoaiTK == "2")
            {
                Session.Add("username", tk.TenTaiKhoan);
                Session.Add("userid", maTaiKhoan);
                string uri = Request.RawUrl;
                //uri = Regex.Replace(uri, "/Guest/", "/Member/");
                //Response.Redirect(uri);
                Response.Redirect("~/Member/HomePage.aspx"); //dung
            }
            else if (result == true && maLoaiTK == "3")
            {
                Session.Add("username", tk.TenTaiKhoan);
                Session.Add("userid", maTaiKhoan);
                string uri = Request.RawUrl;
                Response.Redirect("~/Manager/HomePage.aspx"); //dung
            }
            else if (result == true && maLoaiTK == "4")
            {
                Session.Add("username", tk.TenTaiKhoan);
                Session.Add("userid", maTaiKhoan);
                string uri = Request.RawUrl;
                Response.Redirect("~/Admin/QuanLyTaiKhoan.aspx"); //dung
            }
            else
            {
                Session.Add("previousUri", Request.RawUrl);
                Response.Redirect("/Guest/LoginPage.aspx");
            }
        }
        protected void btnTimKiemNangCao_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Guest/TimKiemNangCao.aspx");
        }
    }
}
